home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / jpsrc2.zip / JWRPPM.C < prev    next >
C/C++ Source or Header  |  1991-12-02  |  5KB  |  178 lines

  1. /*
  2.  * jwrppm.c
  3.  *
  4.  * Copyright (C) 1991, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to write output images in PPM format.
  9.  * The PBMPLUS library is required (well, it will be in the real version).
  10.  *
  11.  * These routines may need modification for non-Unix environments or
  12.  * specialized applications.  As they stand, they assume output to
  13.  * an ordinary stdio stream.
  14.  *
  15.  * These routines are invoked via the methods put_pixel_rows, put_color_map,
  16.  * and output_init/term.
  17.  */
  18.  
  19. #include "jinclude.h"
  20.  
  21. #ifdef PPM_SUPPORTED
  22.  
  23.  
  24. /*
  25.  * Haven't yet got around to making this work with text-format output,
  26.  * hence cannot handle pixels wider than 8 bits.
  27.  */
  28.  
  29. #ifndef EIGHT_BIT_SAMPLES
  30.   Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
  31. #endif
  32.  
  33.  
  34. static JSAMPARRAY color_map;    /* saves color map passed by quantizer */
  35.  
  36.  
  37. /*
  38.  * Write the file header.
  39.  */
  40.  
  41. METHODDEF void
  42. output_init (decompress_info_ptr cinfo)
  43. {
  44.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  45.     /* emit header for raw PGM format */
  46.     fprintf(cinfo->output_file, "P5\n%ld %ld\n%d\n",
  47.         cinfo->image_width, cinfo->image_height, 255);
  48.   } else if (cinfo->out_color_space == CS_RGB) {
  49.     /* emit header for raw PPM format */
  50.     fprintf(cinfo->output_file, "P6\n%ld %ld\n%d\n",
  51.         cinfo->image_width, cinfo->image_height, 255);
  52.   } else {
  53.     ERREXIT(cinfo->emethods, "PPM output must be grayscale or RGB");
  54.   }
  55. }
  56.  
  57.  
  58. /*
  59.  * Write some pixel data.
  60.  */
  61.  
  62. METHODDEF void
  63. put_pixel_rows (decompress_info_ptr cinfo, int num_rows,
  64.         JSAMPIMAGE pixel_data)
  65. {
  66.   register FILE * outfile = cinfo->output_file;
  67.   register JSAMPROW ptr0, ptr1, ptr2;
  68.   register long col;
  69.   register long width = cinfo->image_width;
  70.   register int row;
  71.   
  72.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  73.     for (row = 0; row < num_rows; row++) {
  74.       ptr0 = pixel_data[0][row];
  75.       for (col = width; col > 0; col--) {
  76.     putc(GETJSAMPLE(*ptr0), outfile);
  77.     ptr0++;
  78.       }
  79.     }
  80.   } else {
  81.     for (row = 0; row < num_rows; row++) {
  82.       ptr0 = pixel_data[0][row];
  83.       ptr1 = pixel_data[1][row];
  84.       ptr2 = pixel_data[2][row];
  85.       for (col = width; col > 0; col--) {
  86.     putc(GETJSAMPLE(*ptr0), outfile);
  87.     ptr0++;
  88.     putc(GETJSAMPLE(*ptr1), outfile);
  89.     ptr1++;
  90.     putc(GETJSAMPLE(*ptr2), outfile);
  91.     ptr2++;
  92.       }
  93.     }
  94.   }
  95. }
  96.  
  97.  
  98. /*
  99.  * Write some pixel data when color quantization is in effect.
  100.  */
  101.  
  102. METHODDEF void
  103. put_demapped_rows (decompress_info_ptr cinfo, int num_rows,
  104.            JSAMPIMAGE pixel_data)
  105. {
  106.   register FILE * outfile = cinfo->output_file;
  107.   register JSAMPROW ptr;
  108.   register long col;
  109.   register long width = cinfo->image_width;
  110.   register int row;
  111.   
  112.   if (cinfo->out_color_space == CS_GRAYSCALE) {
  113.     for (row = 0; row < num_rows; row++) {
  114.       ptr = pixel_data[0][row];
  115.       for (col = width; col > 0; col--) {
  116.     putc(GETJSAMPLE(color_map[0][GETJSAMPLE(*ptr)]), outfile);
  117.     ptr++;
  118.       }
  119.     }
  120.   } else {
  121.     for (row = 0; row < num_rows; row++) {
  122.       ptr = pixel_data[0][row];
  123.       for (col = width; col > 0; col--) {
  124.     register int pixval = GETJSAMPLE(*ptr);
  125.  
  126.     putc(GETJSAMPLE(color_map[0][pixval]), outfile);
  127.     putc(GETJSAMPLE(color_map[1][pixval]), outfile);
  128.     putc(GETJSAMPLE(color_map[2][pixval]), outfile);
  129.     ptr++;
  130.       }
  131.     }
  132.   }
  133. }
  134.  
  135.  
  136. /*
  137.  * Write the color map.
  138.  * For PPM output, we just demap the output data!
  139.  */
  140.  
  141. METHODDEF void
  142. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  143. {
  144.   color_map = colormap;        /* save for use in output */
  145.   cinfo->methods->put_pixel_rows = put_demapped_rows;
  146. }
  147.  
  148.  
  149. /*
  150.  * Finish up at the end of the file.
  151.  */
  152.  
  153. METHODDEF void
  154. output_term (decompress_info_ptr cinfo)
  155. {
  156.   /* No work except to make sure we wrote the output file OK */
  157.   fflush(cinfo->output_file);
  158.   if (ferror(cinfo->output_file))
  159.     ERREXIT(cinfo->emethods, "Output file write error");
  160. }
  161.  
  162.  
  163. /*
  164.  * The method selection routine for PPM format output.
  165.  * This should be called from d_ui_method_selection if PPM output is wanted.
  166.  */
  167.  
  168. GLOBAL void
  169. jselwppm (decompress_info_ptr cinfo)
  170. {
  171.   cinfo->methods->output_init = output_init;
  172.   cinfo->methods->put_color_map = put_color_map;
  173.   cinfo->methods->put_pixel_rows = put_pixel_rows;
  174.   cinfo->methods->output_term = output_term;
  175. }
  176.  
  177. #endif /* PPM_SUPPORTED */
  178.